﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

namespace MediawikiSPMigrator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        Dictionary<string, Dictionary<string, string>> templateDetails;

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MediawikiSPMigratorClass.WikiData wikiData = new MediawikiSPMigratorClass.WikiData();
            string output = wikiData.FormatWikiText(WikiText.Text, out templateDetails);
            
            HtmlViewer.NavigateToString(output);
            HtmlText.Text = output;
        }

        private void LoadLibsSite_Click(object sender, RoutedEventArgs e)
        {
            using (SPSite site = new SPSite(SPSiteText.Text))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SiteList.Items.Clear();
                    foreach (SPList list in web.Lists)
                    {
                        SiteList.Items.Add(list);
                    }
                }
            }
        }

        private void CreatePageButton_Click(object sender, RoutedEventArgs e)
        {
            string pageFileName = PageNameText.Text;
            SPList selectedList = SiteList.SelectedItem as SPList;
            
            string applicationPageLayout = string.Empty;
            if (selectedList != null) {
                try
                {
                    if (templateDetails != null && selectedList != null)
                    {
                        MediawikiSPMigratorClass.SharePointData spData = new MediawikiSPMigratorClass.SharePointData(selectedList, templateDetails);

                        spData.CreatePage(pageFileName, WikiText.Text);
                    }
                }
                catch (Exception exp) 
                {
                    ErrorText.Visibility = System.Windows.Visibility.Visible;
                    ErrorText.Text = exp.Message;
                }
            }
        }
    }

}
